using System.Collections.Generic;
using BCW;
using UnityEngine;

public class SelectableButtonShop : SelectableButton
{
	[SerializeField]
	private UIBasicSprite picture;

	[SerializeField]
	private GameObject equipBG;

	[SerializeField]
	private GameObject unequipBG;

	[SerializeField]
	private MarketPriceTag selectedPriceTag;

	[SerializeField]
	private MarketPriceTag unselectedPriceTag;

	[SerializeField]
	private GameObject saleTag;

	[SerializeField]
	private UILabel saleTagLabel;

	[SerializeField]
	private GameObject buyButton;

	[SerializeField]
	private GameObject priceTag;

	private bool _isBuy;

	private MarketMiniProductInfo _transportInfoPanel;

	private SelectTransportUI _selectTransportController;

	private SelectableButtonController _selectableButtonController;

	private Product _productToBuy;

	public MarketMiniProductInfo TransportInfoPanel
	{
		get
		{
			return _transportInfoPanel;
		}
		set
		{
			_transportInfoPanel = value;
		}
	}

	public SelectTransportUI SelectTransportController
	{
		get
		{
			return _selectTransportController;
		}
		set
		{
			_selectTransportController = value;
		}
	}

	public SelectableButtonController SelectableButtonController
	{
		get
		{
			return _selectableButtonController;
		}
		set
		{
			_selectableButtonController = value;
		}
	}

	private void reset()
	{
		_isBuy = false;
		Unequip();
		notSelectedBG.SetActive(true);
		buyButton.SetActive(true);
		priceTag.SetActive(true);
	}

	public void InitButton(Product product)
	{
		reset();
		SetBuyButtonInfo(product);
		SetSaleInfo();
		LoadProductTexture();
	}

	public override void Select(bool select)
	{
		if (_isBuy)
		{
			Equip();
		}
		if (select && !selected)
		{
			SetBottomPanelInfo();
		}
		base.Select(select);
	}

	public void Unequip()
	{
		equipBG.SetActive(false);
		selectedBG.SetActive(false);
		notSelectedBG.SetActive(false);
		unequipBG.SetActive(true);
	}

	private void SetBottomPanelInfo()
	{
		TransportInfoPanel.Product = _productToBuy;
		TransportInfoPanel.ShowMiniInfoPanel();
	}

	public void Buy()
	{
		if (MarketController.Instance.BuyProduct(_productToBuy))
		{
			SendStatistics(_productToBuy);
			if (_productToBuy is ProductComsumable)
			{
				SetBottomPanelInfo();
				return;
			}
			_isBuy = true;
			buyButton.SetActive(false);
			priceTag.SetActive(false);
			Equip();
		}
		else
		{
			StartCoroutine(MarketControllerUI.instance.ShowBank(_productToBuy.PriceIn));
		}
	}

	private void Equip()
	{
		if ((bool)_selectTransportController)
		{
			if ((bool)_selectTransportController.BoughtShopButton)
			{
				_selectTransportController.BoughtShopButton.Unequip();
			}
			_selectTransportController.BoughtShopButton = this;
		}
		if ((bool)_selectableButtonController && (bool)_selectableButtonController.selectedButton)
		{
			_selectableButtonController.selectedButton.Select(false);
		}
		equipBG.SetActive(true);
		selectedBG.SetActive(false);
		notSelectedBG.SetActive(false);
		saleTag.SetActive(false);
		MarketController.Instance.Equip(_productToBuy);
	}

	private void SetSaleInfo()
	{
		if (_productToBuy != null && _productToBuy.sale != null && _productToBuy.SalePercent != 0f)
		{
			saleTag.SetActive(true);
			saleTagLabel.text = _productToBuy.SalePercent + "%";
		}
		else
		{
			saleTag.SetActive(false);
		}
	}

	private void SetBuyButtonInfo(Product product)
	{
		if (CupsManager.CurrentTier < product.tier)
		{
			return;
		}
		if (!product.Purchased)
		{
			if (product.availableForBuy)
			{
				_productToBuy = product;
				selectedPriceTag.SetPrice(_productToBuy);
				unselectedPriceTag.SetPrice(_productToBuy);
			}
		}
		else if (product.nextProduct != null && !product.nextProduct.Purchased)
		{
			_productToBuy = product.nextProduct;
			if (CupsManager.CurrentTier >= _productToBuy.tier)
			{
				selectedPriceTag.SetPrice(_productToBuy);
				unselectedPriceTag.SetPrice(_productToBuy);
			}
		}
	}

	private void LoadProductTexture()
	{
		picture = picture.ReplaceForCurrentPlatform();
		if (_productToBuy != null)
		{
			picture.LoadTextureAndAlphaMask("AtlasShopGuns/", _productToBuy.texture);
		}
	}

	private void SendStatistics(Product product)
	{
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		dictionary.Clear();
		dictionary.Add("Buying_item", product.id);
		FlurryWrapper.LogEvent(FlurryWrapper.EV_RECOMENDED_ITEMS_FOR_MODE, dictionary);
	}
}
